package me.raih.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author Raih
 * @Time 2020/11/27  13:38
 *
 * 后台 管理员登录拦截器
 */
public class AdminHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取登录的session
        Object admin_flag = request.getSession().getAttribute("ADMIN_LOGIN_SESSION_FLAG");

                //只有 只有一个不为空才能登录
        if(admin_flag == null){
            //如果不存在就是没登录
            //不仅仅不能登录，还要把已经存在的session都去掉
            //request.getSession().invalidate();
            //因为这里期望是地址栏的改变，所以用了response
            response.sendRedirect("/admin/login");
            return false;
        }else {
            return true;
        }
    }
}
